package com.cqc.retrofitdemo01;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.orhanobut.logger.Logger;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = "MainActivity";
    private Button btn1;
    private Button btn2;
    private Button btn3;
    private Button btn4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViews();
        initViews();

    }

    private void initViews() {
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
        btn4.setOnClickListener(this);
    }

    private void findViews() {
        btn1 = (Button) findViewById(R.id.btn1);
        btn2 = (Button) findViewById(R.id.btn2);
        btn3 = (Button) findViewById(R.id.btn3);
        btn4 = (Button) findViewById(R.id.btn4);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn1:
                get1();
                break;
            case R.id.btn2:
                get2();
                break;
            case R.id.btn3:
                get3();
                break;
            case R.id.btn4:
                post1();
                break;

        }
    }

    public void get1() {
        String url = "http://192.168.1.11:8080/QQ/";
        //1 创建Retrofit对象
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(url)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        //2 获取网络接口 AppUrl
        AppUrl appUrl = retrofit.create(AppUrl.class);
        //3 调用AppUrl里面的方法获取call
        Call<UpdateBean> call = appUrl.get1();
        //4 开始请求
        call.enqueue(new Callback<UpdateBean>() {
            @Override
            public void onResponse(Call<UpdateBean> call, Response<UpdateBean> response) {
                //response.body()就是泛型
                UpdateBean bean = response.body();
                Logger.d("id=" + bean.getId());
            }

            @Override
            public void onFailure(Call<UpdateBean> call, Throwable t) {
                Logger.d("message=" + t.getMessage() + "\n toString=" + t.toString());
            }
        });
    }

    //<>有泛型，那么必须添加方法addConverterFactory(GsonConverterFactory.create())
    public void get2() {
        //http://apis.juhe.cn/ip/ip2addr?=www.juhe.cn&key=appkey
        String baseUrl = "http://apis.juhe.cn/ip/";
        //1 创建retrogit对象
        Retrofit retrofit = new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(baseUrl)
                .build();
        //2 获取appUrl
        AppUrl appUrl = retrofit.create(AppUrl.class);
        //3 获取call
        Call<IPBean> call = appUrl.get2("192.168.1.12", "25de166737ce8c442bbcd70afc44f665");
        //4 开始请求
        call.enqueue(new Callback<IPBean>() {
            @Override
            public void onResponse(Call<IPBean> call, Response<IPBean> response) {
                Logger.d("location=" + response.body().getResult().getLocation());
            }

            @Override
            public void onFailure(Call<IPBean> call, Throwable t) {
                Logger.d("message=" + t.getMessage() + "\n toString=" + t.toString());
            }
        });
    }

    public void post1() {
        //http://apis.juhe.cn/ip/ip2addr?=www.juhe.cn&key=appkey
        String baseUrl = "http://apis.juhe.cn/ip/";
        //1 创建retrogit对象
        Retrofit retrofit = new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(baseUrl)
                .build();
        //2 获取appUrl
        AppUrl appUrl = retrofit.create(AppUrl.class);
        //3 获取call
        Call<IPBean> call = appUrl.post1("192.168.1.12", "25de166737ce8c442bbcd70afc44f665");
        //4 开始请求
        call.enqueue(new Callback<IPBean>() {
            @Override
            public void onResponse(Call<IPBean> call, Response<IPBean> response) {
                Logger.d("location=" + response.body().getResult().getLocation());
            }

            @Override
            public void onFailure(Call<IPBean> call, Throwable t) {
                Logger.d("message=" + t.getMessage() + "\n toString=" + t.toString());
            }
        });
    }

    public void get3() {
        //http://apis.juhe.cn/ip/ip2addr?=www.juhe.cn&key=appkey
        String baseUrl = "http://apis.juhe.cn/ip/";
        //1 创建retrogit对象
        Retrofit retrofit = new Retrofit.Builder()
                .addConverterFactory(ScalarsConverterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(baseUrl)
                .build();
        //2 获取appUrl
        AppUrl appUrl = retrofit.create(AppUrl.class);
        //3 获取call
        Call<String> call = appUrl.get3("192.168.1.12", "25de166737ce8c442bbcd70afc44f665");
        //4 执行请求
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                Logger.d("response=" + response.body().toString());
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                Logger.d("message=" + t.getMessage() + "\n toString=" + t.toString() + "\n" + "code=" + t.hashCode());
                if (t != null && t.getMessage().startsWith("Failed to connect")) { //Unable to resolve host
                    Logger.d("请检查网络是否畅通");
                }
            }
        });

    }
}
